package com.atguigu.tingshu.order.template;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderDerate;
import com.atguigu.tingshu.model.order.OrderDetail;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.mapper.OrderDerateMapper;
import com.atguigu.tingshu.order.mapper.OrderDetailMapper;
import com.atguigu.tingshu.order.mapper.OrderInfoMapper;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.AccessType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class SaveOrderTemplate {

    @Autowired
    private OrderInfoMapper orderInfoMapper ;

    @Autowired
    private OrderDetailMapper orderDetailMapper ;

    @Autowired
    private OrderDerateMapper orderDerateMapper ;

    /**
     * @description:
     * @author Petrichor
     * @param orderInfoVo 订单对象
     * @param tradeNo     UUID 随机生成的 订单id
     * @date 2024/1/8 16:43
     **/
    @Transactional
    // 保存订单的相关数据(订单的基本数据、明细数据、减免明细数据)
    public void saveOrderAndDetailAndOrderDerate(OrderInfoVo orderInfoVo, String tradeNo) {

        // 获取当前登录用户id
        Long userId = AuthContextHolder.getUserId();

        // 保存订单主表 order_info
        OrderInfo orderInfo = new OrderInfo();

        BeanUtils.copyProperties(orderInfoVo, orderInfo);
        orderInfo.setUserId(userId);
        String itemName = orderInfoVo.getOrderDetailVoList().get(0).getItemName();
        orderInfo.setOrderTitle(itemName);
        orderInfo.setOrderNo(tradeNo);
        // 未支付
        orderInfo.setOrderStatus(SystemConstant.ORDER_STATUS_UNPAID);
        orderInfoMapper.insert(orderInfo);

        // 保存订单明细 order_detail
        if (!CollectionUtils.isEmpty(orderInfoVo.getOrderDetailVoList())) {
            orderInfoVo.getOrderDetailVoList().forEach(orderDetailVo -> {
                OrderDetail orderDetail = new OrderDetail();
                BeanUtils.copyProperties(orderDetailVo, orderDetail);
                orderDetail.setOrderId(orderInfo.getId());
                orderDetailMapper.insert(orderDetail);
            });
        }
        // 保存订单减免 order_derate
        if (!CollectionUtils.isEmpty(orderInfoVo.getOrderDerateVoList())) {
            orderInfoVo.getOrderDerateVoList().forEach(orderDerateVo -> {
                OrderDerate orderDerate = new OrderDerate();
                BeanUtils.copyProperties(orderDerateVo, orderDerate);
                orderDerate.setOrderId(orderInfo.getId());
                orderDerateMapper.insert(orderDerate);
            });
        }

        // 将新的订单号，设置给orderInfoVo对象
        orderInfoVo.setTradeNo(tradeNo);

        // 后续操作方法
        this.saveOrderAfterProcess(tradeNo);

    }

    public void saveOrderAfterProcess(String orderNo) {}

}